# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Zach Bacon <zachbacon@vba-m.com>

_realname=gpgme
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-qgpgme-qt5"
         "${MINGW_PACKAGE_PREFIX}-python-gpgme")
pkgver=1.22.0
pkgrel=1
pkgdesc="A C wrapper library for GnuPG (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gnupg.org/related_software/gpgme/"
license=('spdx: LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gnupg"
         "${MINGW_PACKAGE_PREFIX}-libassuan"
         "${MINGW_PACKAGE_PREFIX}-libgpg-error"
         "${MINGW_PACKAGE_PREFIX}-npth")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "doxygen")
source=("https://www.gnupg.org/ftp/gcrypt/${_realname}/${_realname}-${pkgver}.tar.bz2"{,.sig}
        0004-gpgme-find-gnupg.patch
        0005-invoke-scripts-via-sh.patch
        0007-mkdefsinc-use-CPPFLAGS.patch
        0008-no-py2.patch
        0009-fix-broken-version.patch
        0010-m4-check-python-exit-code.patch
        relocatable-cmake.patch
        gpgmepp-portable-types.patch
        gpgme-fix-cpp-tests.patch)
#These might be signed by any of these keys https://gnupg.org/signature_key.html
validpgpkeys=('5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'
              '6DAA6E64A76D2840571B4902528897B826403ADA'
              'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD'
              '02F38DFF731FF97CB039A1DA549E695E905BA208')
sha256sums=('9551e37081ad3bde81018a0d24f245c3f8206990549598fb31a97a68380a7b71'
            'SKIP'
            'c8e4b6a64f71ceb8a84f177829ce7e96f983881ffe8e7e952de3b255a8e20143'
            '75f77086c96fb4bcb9edad01bf64bbf11a7ec435f751b901f3fb180b410fa236'
            'f022993b4ae8f69f8248a6e22a0b6f0d0f43a074cdbcdbe2efb479e48064f988'
            '1fa234be3412c7664110900de2074b36c88d39c79bbc6ec7870fd6b76f7cb5e4'
            'ace3d5166372e0422a625f76a2890d70f2916d9239ade74e58bd23077e9c1c6b'
            '5e8e7e773a09ed1e1724c9d7216eaaecce52af49152513366026a2fa3245d762'
            '81f412ffdeeb2a0757248b517fbda9caae9913b1552c88abb8347faa4d1efea2'
            '645f019ff078b26e6badbc238a440a4430ada2e48d8f6eda715a76546e2a5848'
            'd09070c005b72e8aed14a848553f7c7289353ff7f32e62c9013a729ba46d7117')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    0004-gpgme-find-gnupg.patch \
    0005-invoke-scripts-via-sh.patch \
    0007-mkdefsinc-use-CPPFLAGS.patch \
    0008-no-py2.patch \
    0009-fix-broken-version.patch \
    0010-m4-check-python-exit-code.patch \
    relocatable-cmake.patch \
    gpgmepp-portable-types.patch \
    gpgme-fix-cpp-tests.patch

  autoreconf -ivf
}

build() {
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  # for clang
  export CFLAGS="${CFLAGS} -Wno-int-conversion -Wno-incompatible-function-pointer-types"

  # mingw doxygen can't wrok with UNIX paths in doxyfile
  # --with-libtool-modification=never : Otherwise DLL name changes
  # see cmake files for the expected DLL names
  DOXYGEN=/usr/bin/doxygen \
  PYTHON=${MINGW_PREFIX}/bin/python3 \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --libexecdir=${MINGW_PREFIX}/bin \
    --disable-fd-passing \
    --enable-shared \
    --enable-static \
    --disable-gpgsm-test \
    --disable-gpgconf-test \
    --disable-gpg-test \
    --enable-w32-glib \
    --with-libtool-modification=never

  make

  (
    # use a PEP517 workflow to get a reproducible Python package
    # NOTE: top_builddir is required so that the build takes place against local gpgme, not system gpgme
    cd lang/python/
    top_builddir="${srcdir}"/build-${MSYSTEM} \
    ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
  )
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check || warning "Tests failed"
}

package_gpgme() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LESSER" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gpgme-config"

  # split qgpgme
  rm -r "${pkgdir}${MINGW_PREFIX}"/bin/libqgpgme*
  rm -r "${pkgdir}${MINGW_PREFIX}"/lib/{cmake/QGpgme/,libqgpgme.*}
  rm -r "${pkgdir}${MINGW_PREFIX}"/lib/python*
}

package_qgpgme-qt5() {
  pkgdesc="Qt5 bindings for GPGme (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gpgme"
           "${MINGW_PACKAGE_PREFIX}-qt5-base")

  cd "${srcdir}"/build-${MSYSTEM}/lang/qt
  make DESTDIR="${pkgdir}" install
  rm -r "${pkgdir}${MINGW_PREFIX}"/include
}

package_python-gpgme() {
  pkgdesc="Python bindings for GPGme (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gpgme"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}"/build-${MSYSTEM}/lang/python

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
